package com.person.controller;

import com.person.common.R;
import com.person.pojo.Category;
import com.person.pojo.PageBean;
import com.person.service.CategoryService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

@RestController
@RequestMapping("/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    //添加菜品分类
    @PostMapping
    public R<String> addCategory(@RequestBody Category category){
        log.info("新增菜品分类：{}",category);
        categoryService.addCategory(category);
        return R.success("添加成功");
    }

    //分页查看
    @GetMapping("/page")
    public R<PageBean> page(@RequestParam(defaultValue = "1") Integer page,
                          @RequestParam(defaultValue = "10") Integer pageSize){
        log.info("分页页码{}，页数{}",page,pageSize);

        PageBean categories= categoryService.page(page,pageSize);
        log.info("分页查看{}",categories.toString() );
        return R.success(categories);
    }

    //删除分类
    @DeleteMapping
    public R<String> deleteCategory( Long ids){
        log.info("要删除分类的id是：{}",ids);
        categoryService.deleteCateGory(ids);
        return R.success("删除成功");

    }

    //修改分类
    @PutMapping
    public R<String> updateCategory(@RequestBody Category category){
        log.info("修改分类为{}",category);
        categoryService.update(category);
        return R.success("修改成功");

    }
    /**
     * 菜品分类下拉框展示
     */
    @GetMapping("/list")
    public R<List> categoryList(Category category){
            List<Category> categories = categoryService.categoryList(category);
        return R.success(categories);
    }
}
